-
Notifications
You must be signed in to change notification settings - Fork 0
KAFKA-19160: Improve performance of fetching stable offsets #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: trunk
Are you sure you want to change the base?
Conversation
When fetching stable offsets in the group coordinator, we iterate over all requested partitions. For each partition, we iterate over the group's ongoing transactions to check if there is a pending transactional offset commit for that partition. This can get slow when there are a large number of partitions and a large number of pending transactions. Instead, maintain a list of pending transactions per partition to speed up lookups.
@squah-confluent Thanks for the patch. Could we write a micro benchmark to demonstrate the gain? |
WalkthroughA new nested data structure was introduced in the Changes
Sequence Diagram(s)sequenceDiagram
participant Client
participant OffsetMetadataManager
participant Storage
Client->>OffsetMetadataManager: Commit Transactional Offset (group, topic, partition, producerId)
OffsetMetadataManager->>OffsetMetadataManager: Add producerId to openTransactionsByGroupTopicAndPartition
OffsetMetadataManager->>Storage: Store transactional offset
Client->>OffsetMetadataManager: Delete All Offsets (group)
OffsetMetadataManager->>OffsetMetadataManager: Iterate openTransactionsByGroupTopicAndPartition
OffsetMetadataManager->>Storage: Add tombstone if no committed offset
Client->>OffsetMetadataManager: Replay Transactional Offset Commit
OffsetMetadataManager->>OffsetMetadataManager: Update nested map with producerId
Client->>OffsetMetadataManager: End Transaction (producerId)
OffsetMetadataManager->>OffsetMetadataManager: Remove producerId from nested map
OffsetMetadataManager->>OffsetMetadataManager: Clean up empty nested maps
Poem
Tip ⚡💬 Agentic Chat (Pro Plan, General Availability)
✨ Finishing Touches
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 1
🧹 Nitpick comments (3)
group-coordinator/src/main/java/org/apache/kafka/coordinator/group/OffsetMetadataManager.java (3)
290-292
: Consider initial capacity hintWhile the new map is created with default capacity, we already know the upper bound equals the number of groups in the shard.
Passing an estimated initial capacity (e.g.new TimelineHashMap<>(snapshotRegistry, expectedGroupCount)
) would avoid internal re‑hashing during warm‑up and complement the performance goal.
690-699
: Minor micro‑optimisation opportunity
hasPendingTransactionalOffsets
currently performs two nested look‑ups even when the group isn’t present.
Inlining the fast‑fail path keeps the common case cheap:TimelineHashMap<String, TimelineHashMap<Integer, TimelineHashSet<Long>>> topics = openTransactionsByGroupTopicAndPartition.get(groupId); if (topics == null) return false; TimelineHashMap<Integer, TimelineHashSet<Long>> partitions = topics.get(topic); return partitions != null && !partitions.getOrDefault(partition, TimelineHashSet.empty()).isEmpty();Not critical, but consistent with the goal of shaving latency off hot paths.
1008-1012
: Helper method could improve readability & symmetryThe three‑level
computeIfAbsent
chain is repeated in multiple places (here, plus future writes).
Encapsulating it in a privateaddOpenTransaction(groupId, topic, partition, producerId)
helper removes duplication and lowers the cognitive load for future maintainers.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
group-coordinator/src/main/java/org/apache/kafka/coordinator/group/OffsetMetadataManager.java
(7 hunks)
🔇 Additional comments (2)
group-coordinator/src/main/java/org/apache/kafka/coordinator/group/OffsetMetadataManager.java (2)
201-205
: 👍 Valuable fine‑grained tracking addedIntroducing
openTransactionsByGroupTopicAndPartition
is a solid design choice that avoids repeatedly scanning every producer of a group when we only care about a single partition.
This lays the groundwork for the performance win described in the PR objective.
1067-1100
: Edge‑case: staleopenTransactionsByGroup
entry
replayEndTransactionMarker
correctly removes a producer‑id from both structures, butreplay
’s tombstone path (lines 1020‑1041) updates only the partition‑level map.
If that tombstone happens to clear the last pending offset for the producer, the group‑level set will still contain an orphaned producer id until the transaction eventually ends.
This can postpone group cleanup viacleanupExpiredOffsets
.If intentional, a comment explaining the lifecycle would help; otherwise consider synchronising removals in both places.
TimelineHashMap<String, TimelineHashMap<Integer, TimelineHashSet<Long>>> openTransactionsByTopic = | ||
openTransactionsByGroupTopicAndPartition.get(groupId); | ||
if (openTransactionsByTopic != null) { | ||
openTransactionsByTopic.forEach((topic, openTransactionsByPartition) -> { | ||
openTransactionsByPartition.forEach((partition, producerIds) -> { | ||
producerIds.forEach(producerId -> { | ||
if (!hasCommittedOffset(groupId, topic, partition)) { | ||
records.add(GroupCoordinatorRecordHelpers.newOffsetCommitTombstoneRecord(groupId, topic, partition)); | ||
numDeletedOffsets.getAndIncrement(); | ||
} | ||
}); | ||
}); | ||
}); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Duplicate tombstones may be emitted for the same partition
producerIds.forEach(...)
iterates once per producer id, potentially adding the same tombstone several times when multiple producers have pending offsets for the identical <group, topic, partition>
.
This inflates records
and over‑counts numDeletedOffsets
, causing needless log traffic and skewed metrics.
- openTransactionsByPartition.forEach((partition, producerIds) -> {
- producerIds.forEach(producerId -> {
- if (!hasCommittedOffset(groupId, topic, partition)) {
- records.add(GroupCoordinatorRecordHelpers
- .newOffsetCommitTombstoneRecord(groupId, topic, partition));
- numDeletedOffsets.getAndIncrement();
- }
- });
- });
+ openTransactionsByPartition.keySet().forEach(partition -> {
+ if (!hasCommittedOffset(groupId, topic, partition)) {
+ records.add(GroupCoordinatorRecordHelpers
+ .newOffsetCommitTombstoneRecord(groupId, topic, partition));
+ numDeletedOffsets.getAndIncrement();
+ }
+ });
This preserves the original semantics while guaranteeing a single tombstone per partition.
/gemini review |
Warning Gemini encountered an error creating the review. You can try again by commenting |
/gemini review |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
This pull request introduces a performance improvement for fetching stable offsets by maintaining a list of pending transactions per partition. The changes seem well-structured and address the identified bottleneck. However, some areas could benefit from further clarification and potential optimization.
Summary of Findings
- Missing null check: In
replayEndTransactionMarker
, there's a potential null pointer exception ifopenTransactionsByTopic
is null after removing a topic. A null check before proceeding with further operations would enhance robustness. - Potential memory leak: In
replay
, when removing pending offset commits, the code removes partitions and topics fromopenTransactionsByGroupTopicAndPartition
. However, it's crucial to ensure that theOffsets
object associated withpendingTransactionalOffsets
is also cleaned up to prevent a potential memory leak.
Merge Readiness
The pull request introduces a significant performance improvement and appears to be well-structured. However, the potential null pointer exception and memory leak should be addressed before merging. I am unable to approve this pull request, and recommend that others review and approve this code before merging. At a minimum, the high severity issues should be addressed before merging.
When fetching stable offsets in the group coordinator, we iterate over
all requested partitions. For each partition, we iterate over the
group's ongoing transactions to check if there is a pending
transactional offset commit for that partition.
This can get slow when there are a large number of partitions and a
large number of pending transactions. Instead, maintain a list of
pending transactions per partition to speed up lookups.
Summary by CodeRabbit
New Features
Bug Fixes
Performance